package leetcode.code1638;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int countSubstrings(String s, String t) {
		char[] ss = s.toCharArray();
		char[] tt = t.toCharArray();
		int len1 = s.length();
		int len2 = t.length();
		int ans = 0;
		for (int size = 1; size <= Math.min(len1, len2); size++) {
			Map<String, Integer> h1 = new HashMap<>();
			Map<String, Integer> h2 = new HashMap<>();
			for (int i = 0; i < len1 - size + 1; i++) {
				String str = new String(ss, i, size);
				h1.put(str, h1.getOrDefault(str, 0) + 1);
			}
			for (int i = 0; i < len2 - size + 1; i++) {
				String str = new String(tt, i, size);
				h2.put(str, h2.getOrDefault(str, 0) + 1);
			}
			for (Map.Entry<String, Integer> e1 : h1.entrySet()) {
				String s1 = e1.getKey();
				int n1 = e1.getValue();
				for (Map.Entry<String, Integer> e2 : h2.entrySet()) {
					int p = 0, cnt = 0;
					String s2 = e2.getKey();
					int n2 = e2.getValue();
					while (p < size) {
						if (s1.charAt(p) != s2.charAt(p)) {
							cnt++;
						}
						if (cnt > 1) {
							break;
						}
						p++;
					}
					if (cnt == 1) {
						ans += n1 * n2;
					}
				}
			}

		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
